home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / foo / expread.y < prev    next >
Encoding:
Text File  |  1990-01-10  |  40.2 KB  |  1,765 lines

  1. /* Parse C expressions for GDB.
  2.    Copyright (C) 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. GDB is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GDB is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GDB; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Parse a C expression from text in a string,
  21.    and return the result as a  struct expression  pointer.
  22.    That structure contains arithmetic operations in reverse polish,
  23.    with constants represented by operations that are followed by special data.
  24.    See expression.h for the details of the format.
  25.    What is important here is that it can be built up sequentially
  26.    during the process of parsing; the lower levels of the tree always
  27.    come first in the result.  */
  28.    
  29. %{
  30. #include <stdio.h>
  31. #include "defs.h"
  32. #include "param.h"
  33. #include "symtab.h"
  34. #include "frame.h"
  35. #include "expression.h"
  36.  
  37. #include <a.out.h>
  38.  
  39. static struct expression *expout;
  40. static int expout_size;
  41. static int expout_ptr;
  42.  
  43. static int yylex ();
  44. static void yyerror ();
  45. static void write_exp_elt ();
  46. static void write_exp_elt_opcode ();
  47. static void write_exp_elt_sym ();
  48. static void write_exp_elt_longcst ();
  49. static void write_exp_elt_dblcst ();
  50. static void write_exp_elt_type ();
  51. static void write_exp_elt_intern ();
  52. static void write_exp_string ();
  53. static void start_arglist ();
  54. static int end_arglist ();
  55. static void free_funcalls ();
  56. static char *copy_name ();
  57.  
  58. /* If this is nonzero, this block is used as the lexical context
  59.    for symbol names.  */
  60.  
  61. static struct block *expression_context_block;
  62.  
  63. /* The innermost context required by the stack and register variables
  64.    we've encountered so far. */
  65. struct block *innermost_block;
  66.  
  67. /* The block in which the most recently discovered symbol was found. */
  68. struct block *block_found;
  69.  
  70. /* Number of arguments seen so far in innermost function call.  */
  71. static int arglist_len;
  72.  
  73. /* Data structure for saving values of arglist_len
  74.    for function calls whose arguments contain other function calls.  */
  75.  
  76. struct funcall
  77.   {
  78.     struct funcall *next;
  79.     int arglist_len;
  80.   };
  81.  
  82. struct funcall *funcall_chain;
  83.  
  84. /* This kind of datum is used to represent the name
  85.    of a symbol token.  */
  86.  
  87. struct stoken
  88.   {
  89.     char *ptr;
  90.     int length;
  91.   };
  92.  
  93. /* For parsing of complicated types.
  94.    An array should be preceded in the list by the size of the array.  */
  95. enum type_pieces
  96.   {tp_end = -1, tp_pointer, tp_reference, tp_array, tp_function};
  97. static enum type_pieces *type_stack;
  98. static int type_stack_depth, type_stack_size;
  99.  
  100. static void push_type ();
  101. static enum type_pieces pop_type ();
  102.  
  103. /* Allow debugging of parsing.  */
  104. #define YYDEBUG 1
  105. %}
  106.  
  107. /* Although the yacc "value" of an expression is not used,
  108.    since the result is stored in the structure being created,
  109.    other node types do have values.  */
  110.  
  111. %union
  112.   {
  113.     LONGEST lval;
  114.     unsigned LONGEST ulval;
  115.     double dval;
  116.     struct symbol *sym;
  117.     struct type *tval;
  118.     struct stoken sval;
  119.     int voidval;
  120.     struct block *bval;
  121.     enum exp_opcode opcode;
  122.     struct internalvar *ivar;
  123.  
  124.     struct type **tvec;
  125.     int *ivec;
  126.   }
  127.  
  128. %type <voidval> exp exp1 start variable
  129. %type <tval> type typebase
  130. %type <tvec> nonempty_typelist
  131. %type <bval> block
  132.  
  133. /* Fancy type parsing.  */
  134. %type <voidval> func_mod direct_abs_decl abs_decl
  135. %type <tval> ptype
  136. %type <lval> array_mod
  137.  
  138. %token <lval> INT CHAR
  139. %token <ulval> UINT
  140. %token <dval> FLOAT
  141.  
  142. /* Both NAME and TYPENAME tokens represent symbols in the input,
  143.    and both convey their data as strings.
  144.    But a TYPENAME is a string that happens to be defined as a typedef
  145.    or builtin type name (such as int or char)
  146.    and a NAME is any other symbol.
  147.  
  148.    Contexts where this distinction is not important can use the
  149.    nonterminal "name", which matches either NAME or TYPENAME.  */
  150.  
  151. %token <sval> NAME TYPENAME BLOCKNAME STRING
  152. %type <sval> name name_not_typename typename
  153.  
  154. %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON
  155.  
  156. /* Special type cases, put in to allow the parser to distinguish different
  157.    legal basetypes.  */
  158. %token SIGNED LONG SHORT INT_KEYWORD
  159.  
  160. %token <lval> LAST REGNAME
  161.  
  162. %token <ivar> VARIABLE
  163.  
  164. %token <opcode> ASSIGN_MODIFY
  165.  
  166. /* C++ */
  167. %token THIS
  168.  
  169. %left ','
  170. %left ABOVE_COMMA
  171. %right '=' ASSIGN_MODIFY
  172. %right '?'
  173. %left OR
  174. %left AND
  175. %left '|'
  176. %left '^'
  177. %left '&'
  178. %left EQUAL NOTEQUAL
  179. %left '<' '>' LEQ GEQ
  180. %left LSH RSH
  181. %left '@'
  182. %left '+' '-'
  183. %left '*' '/' '%'
  184. %right UNARY INCREMENT DECREMENT
  185. %right ARROW '.' '[' '('
  186. %left COLONCOLON
  187.  
  188. %%
  189.  
  190. start   :    exp1
  191.     ;
  192.  
  193. /* Expressions, including the comma operator.  */
  194. exp1    :    exp
  195.     |    exp1 ',' exp
  196.             { write_exp_elt_opcode (BINOP_COMMA); }
  197.     ;
  198.  
  199. /* Expressions, not including the comma operator.  */
  200. exp    :    '*' exp    %prec UNARY
  201.             { write_exp_elt_opcode (UNOP_IND); }
  202.  
  203. exp    :    '&' exp    %prec UNARY
  204.             { write_exp_elt_opcode (UNOP_ADDR); }
  205.  
  206. exp    :    '-' exp    %prec UNARY
  207.             { write_exp_elt_opcode (UNOP_NEG); }
  208.     ;
  209.  
  210. exp    :    '!' exp    %prec UNARY
  211.             { write_exp_elt_opcode (UNOP_ZEROP); }
  212.     ;
  213.  
  214. exp    :    '~' exp    %prec UNARY
  215.             { write_exp_elt_opcode (UNOP_LOGNOT); }
  216.     ;
  217.  
  218. exp    :    INCREMENT exp    %prec UNARY
  219.             { write_exp_elt_opcode (UNOP_PREINCREMENT); }
  220.     ;
  221.  
  222. exp    :    DECREMENT exp    %prec UNARY
  223.             { write_exp_elt_opcode (UNOP_PREDECREMENT); }
  224.     ;
  225.  
  226. exp    :    exp INCREMENT    %prec UNARY
  227.             { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
  228.     ;
  229.  
  230. exp    :    exp DECREMENT    %prec UNARY
  231.             { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
  232.     ;
  233.  
  234. exp    :    SIZEOF exp       %prec UNARY
  235.             { write_exp_elt_opcode (UNOP_SIZEOF); }
  236.     ;
  237.  
  238. exp    :    exp ARROW name
  239.             { write_exp_elt_opcode (STRUCTOP_PTR);
  240.               write_exp_string ($3);
  241.               write_exp_elt_opcode (STRUCTOP_PTR); }
  242.     ;
  243.  
  244. exp    :    exp ARROW '*' exp
  245.             { write_exp_elt_opcode (STRUCTOP_MPTR); }
  246.     ;
  247.  
  248. exp    :    exp '.' name
  249.             { write_exp_elt_opcode (STRUCTOP_STRUCT);
  250.               write_exp_string ($3);
  251.               write_exp_elt_opcode (STRUCTOP_STRUCT); }
  252.     ;
  253.  
  254. exp    :    exp '.' '*' exp
  255.             { write_exp_elt_opcode (STRUCTOP_MEMBER); }
  256.     ;
  257.  
  258. exp    :    exp '[' exp1 ']'
  259.             { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
  260.     ;
  261.  
  262. exp    :    exp '(' 
  263.             /* This is to save the value of arglist_len
  264.                being accumulated by an outer function call.  */
  265.             { start_arglist (); }
  266.         arglist ')'    %prec ARROW
  267.             { write_exp_elt_opcode (OP_FUNCALL);
  268.               write_exp_elt_longcst ((LONGEST) end_arglist ());
  269.               write_exp_elt_opcode (OP_FUNCALL); }
  270.     ;
  271.  
  272. arglist    :
  273.     ;
  274.  
  275. arglist    :    exp
  276.             { arglist_len = 1; }
  277.     ;
  278.  
  279. arglist    :    arglist ',' exp   %prec ABOVE_COMMA
  280.             { arglist_len++; }
  281.     ;
  282.  
  283. exp    :    '{' type '}' exp  %prec UNARY
  284.             { write_exp_elt_opcode (UNOP_MEMVAL);
  285.               write_exp_elt_type ($2);
  286.               write_exp_elt_opcode (UNOP_MEMVAL); }
  287.     ;
  288.  
  289. exp    :    '(' type ')' exp  %prec UNARY
  290.             { write_exp_elt_opcode (UNOP_CAST);
  291.               write_exp_elt_type ($2);
  292.               write_exp_elt_opcode (UNOP_CAST); }
  293.     ;
  294.  
  295. exp    :    '(' exp1 ')'
  296.             { }
  297.     ;
  298.  
  299. /* Binary operators in order of decreasing precedence.  */
  300.  
  301. exp    :    exp '@' exp
  302.             { write_exp_elt_opcode (BINOP_REPEAT); }
  303.     ;
  304.  
  305. exp    :    exp '*' exp
  306.             { write_exp_elt_opcode (BINOP_MUL); }
  307.     ;
  308.  
  309. exp    :    exp '/' exp
  310.             { write_exp_elt_opcode (BINOP_DIV); }
  311.     ;
  312.  
  313. exp    :    exp '%' exp
  314.             { write_exp_elt_opcode (BINOP_REM); }
  315.     ;
  316.  
  317. exp    :    exp '+' exp
  318.             { write_exp_elt_opcode (BINOP_ADD); }
  319.     ;
  320.  
  321. exp    :    exp '-' exp
  322.             { write_exp_elt_opcode (BINOP_SUB); }
  323.     ;
  324.  
  325. exp    :    exp LSH exp
  326.             { write_exp_elt_opcode (BINOP_LSH); }
  327.     ;
  328.  
  329. exp    :    exp RSH exp
  330.             { write_exp_elt_opcode (BINOP_RSH); }
  331.     ;
  332.  
  333. exp    :    exp EQUAL exp
  334.             { write_exp_elt_opcode (BINOP_EQUAL); }
  335.     ;
  336.  
  337. exp    :    exp NOTEQUAL exp
  338.             { write_exp_elt_opcode (BINOP_NOTEQUAL); }
  339.     ;
  340.  
  341. exp    :    exp LEQ exp
  342.             { write_exp_elt_opcode (BINOP_LEQ); }
  343.     ;
  344.  
  345. exp    :    exp GEQ exp
  346.             { write_exp_elt_opcode (BINOP_GEQ); }
  347.     ;
  348.  
  349. exp    :    exp '<' exp
  350.             { write_exp_elt_opcode (BINOP_LESS); }
  351.     ;
  352.  
  353. exp    :    exp '>' exp
  354.             { write_exp_elt_opcode (BINOP_GTR); }
  355.     ;
  356.  
  357. exp    :    exp '&' exp
  358.             { write_exp_elt_opcode (BINOP_LOGAND); }
  359.     ;
  360.  
  361. exp    :    exp '^' exp
  362.             { write_exp_elt_opcode (BINOP_LOGXOR); }
  363.     ;
  364.  
  365. exp    :    exp '|' exp
  366.             { write_exp_elt_opcode (BINOP_LOGIOR); }
  367.     ;
  368.  
  369. exp    :    exp AND exp
  370.             { write_exp_elt_opcode (BINOP_AND); }
  371.     ;
  372.  
  373. exp    :    exp OR exp
  374.             { write_exp_elt_opcode (BINOP_OR); }
  375.     ;
  376.  
  377. exp    :    exp '?' exp ':' exp    %prec '?'
  378.             { write_exp_elt_opcode (TERNOP_COND); }
  379.     ;
  380.               
  381. exp    :    exp '=' exp
  382.             { write_exp_elt_opcode (BINOP_ASSIGN); }
  383.     ;
  384.  
  385. exp    :    exp ASSIGN_MODIFY exp
  386.             { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
  387.               write_exp_elt_opcode ($2);
  388.               write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
  389.     ;
  390.  
  391. exp    :    INT
  392.             { write_exp_elt_opcode (OP_LONG);
  393.               if ($1 == (int) $1 || $1 == (unsigned int) $1)
  394.                 write_exp_elt_type (builtin_type_int);
  395.               else
  396.                 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
  397.               write_exp_elt_longcst ((LONGEST) $1);
  398.               write_exp_elt_opcode (OP_LONG); }
  399.     ;
  400.  
  401. exp    :    UINT
  402.             {
  403.               write_exp_elt_opcode (OP_LONG);
  404.               if ($1 == (unsigned int) $1)
  405.                 write_exp_elt_type (builtin_type_unsigned_int);
  406.               else
  407.                 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
  408.               write_exp_elt_longcst ((LONGEST) $1);
  409.               write_exp_elt_opcode (OP_LONG);
  410.             }
  411.     ;
  412.  
  413. exp    :    CHAR
  414.             { write_exp_elt_opcode (OP_LONG);
  415.               write_exp_elt_type (builtin_type_char);
  416.               write_exp_elt_longcst ((LONGEST) $1);
  417.               write_exp_elt_opcode (OP_LONG); }
  418.     ;
  419.  
  420. exp    :    FLOAT
  421.             { write_exp_elt_opcode (OP_DOUBLE);
  422.               write_exp_elt_type (builtin_type_double);
  423.               write_exp_elt_dblcst ($1);
  424.               write_exp_elt_opcode (OP_DOUBLE); }
  425.     ;
  426.  
  427. exp    :    variable
  428.     ;
  429.  
  430. exp    :    LAST
  431.             { write_exp_elt_opcode (OP_LAST);
  432.               write_exp_elt_longcst ((LONGEST) $1);
  433.               write_exp_elt_opcode (OP_LAST); }
  434.     ;
  435.  
  436. exp    :    REGNAME
  437.             { write_exp_elt_opcode (OP_REGISTER);
  438.               write_exp_elt_longcst ((LONGEST) $1);
  439.               write_exp_elt_opcode (OP_REGISTER); }
  440.     ;
  441.  
  442. exp    :    VARIABLE
  443.             { write_exp_elt_opcode (OP_INTERNALVAR);
  444.               write_exp_elt_intern ($1);
  445.               write_exp_elt_opcode (OP_INTERNALVAR); }
  446.     ;
  447.  
  448. exp    :    SIZEOF '(' type ')'    %prec UNARY
  449.             { write_exp_elt_opcode (OP_LONG);
  450.               write_exp_elt_type (builtin_type_int);
  451.               write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
  452.               write_exp_elt_opcode (OP_LONG); }
  453.     ;
  454.  
  455. exp    :    STRING
  456.             { write_exp_elt_opcode (OP_STRING);
  457.               write_exp_string ($1);
  458.               write_exp_elt_opcode (OP_STRING); }
  459.     ;
  460.  
  461. /* C++.  */
  462. exp    :    THIS
  463.             { write_exp_elt_opcode (OP_THIS);
  464.               write_exp_elt_opcode (OP_THIS); }
  465.     ;
  466.  
  467. /* end of C++.  */
  468.  
  469. block    :    BLOCKNAME
  470.             {
  471.               struct symtab *tem = lookup_symtab (copy_name ($1));
  472.               struct symbol *sym;
  473.               
  474.               if (tem)
  475.                 $$ = BLOCKVECTOR_BLOCK (BLOCKVECTOR (tem), 1);
  476.               else
  477.                 {
  478.                   sym = lookup_symbol (copy_name ($1),
  479.                            expression_context_block,
  480.                            VAR_NAMESPACE, 0);
  481.                   if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
  482.                 $$ = SYMBOL_BLOCK_VALUE (sym);
  483.                   else
  484.                 error ("No file or function \"%s\".",
  485.                        copy_name ($1));
  486.                 }
  487.             }
  488.     ;
  489.  
  490. block    :    block COLONCOLON name
  491.             { struct symbol *tem
  492.                 = lookup_symbol (copy_name ($3), $1, VAR_NAMESPACE, 0);
  493.               if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
  494.                 error ("No function \"%s\" in specified context.",
  495.                    copy_name ($3));
  496.               $$ = SYMBOL_BLOCK_VALUE (tem); }
  497.     ;
  498.  
  499. variable:    block COLONCOLON name
  500.             { struct symbol *sym;
  501.               sym = lookup_symbol (copy_name ($3), $1, VAR_NAMESPACE, 0);
  502.               if (sym == 0)
  503.                 error ("No symbol \"%s\" in specified context.",
  504.                    copy_name ($3));
  505.               write_exp_elt_opcode (OP_VAR_VALUE);
  506.               write_exp_elt_sym (sym);
  507.               write_exp_elt_opcode (OP_VAR_VALUE); }
  508.     ;
  509.  
  510. variable:    typebase COLONCOLON name
  511.             {
  512.               struct type *type = $1;
  513.               if (TYPE_CODE (type) != TYPE_CODE_STRUCT
  514.                   && TYPE_CODE (type) != TYPE_CODE_UNION)
  515.                 error ("`%s' is not defined as an aggregate type.",
  516.                    TYPE_NAME (type));
  517.  
  518.               write_exp_elt_opcode (OP_SCOPE);
  519.               write_exp_elt_type (type);
  520.               write_exp_string ($3);
  521.               write_exp_elt_opcode (OP_SCOPE);
  522.             }
  523.     |    COLONCOLON name
  524.             {
  525.               char *name = copy_name ($2);
  526.               struct symbol *sym;
  527.               int i;
  528.  
  529.               sym = lookup_symbol (name, 0, VAR_NAMESPACE, 0);
  530.               if (sym)
  531.                 {
  532.                   write_exp_elt_opcode (OP_VAR_VALUE);
  533.                   write_exp_elt_sym (sym);
  534.                   write_exp_elt_opcode (OP_VAR_VALUE);
  535.                   break;
  536.                 }
  537.               for (i = 0; i < misc_function_count; i++)
  538.                 if (!strcmp (misc_function_vector[i].name, name))
  539.                   break;
  540.  
  541.               if (i < misc_function_count)
  542.                 {
  543.                   enum misc_function_type mft =
  544.                 (enum misc_function_type)
  545.                   misc_function_vector[i].type;
  546.                   
  547.                   write_exp_elt_opcode (OP_LONG);
  548.                   write_exp_elt_type (builtin_type_int);
  549.                   write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
  550.                   write_exp_elt_opcode (OP_LONG);
  551.                   write_exp_elt_opcode (UNOP_MEMVAL);
  552.                   if (mft == mf_data || mft == mf_bss)
  553.                 write_exp_elt_type (builtin_type_int);
  554.                   else if (mft == mf_text)
  555.                 write_exp_elt_type (lookup_function_type (builtin_type_int));
  556.                   else
  557.                 write_exp_elt_type (builtin_type_char);
  558.                   write_exp_elt_opcode (UNOP_MEMVAL);
  559.                 }
  560.               else
  561.                 if (symtab_list == 0
  562.                 && partial_symtab_list == 0)
  563.                   error ("No symbol table is loaded.  Use the \"symbol-file\" command.");
  564.                 else
  565.                   error ("No symbol \"%s\" in current context.", name);
  566.             }
  567.     ;
  568.  
  569. variable:    name_not_typename
  570.             { struct symbol *sym;
  571.               int is_a_field_of_this;
  572.  
  573.               sym = lookup_symbol (copy_name ($1),
  574.                            expression_context_block,
  575.                            VAR_NAMESPACE,
  576.                            &is_a_field_of_this);
  577.               if (sym)
  578.                 {
  579.                   switch (sym->class)
  580.                 {
  581.                 case LOC_REGISTER:
  582.                 case LOC_ARG:
  583.                 case LOC_LOCAL:
  584.                   if (innermost_block == 0 ||
  585.                       contained_in (block_found, 
  586.                             innermost_block))
  587.                     innermost_block = block_found;
  588.                 }
  589.                   write_exp_elt_opcode (OP_VAR_VALUE);
  590.                   write_exp_elt_sym (sym);
  591.                   write_exp_elt_opcode (OP_VAR_VALUE);
  592.                 }
  593.               else if (is_a_field_of_this)
  594.                 {
  595.                   /* C++: it hangs off of `this'.  Must
  596.                      not inadvertently convert from a method call
  597.                  to data ref.  */
  598.                   if (innermost_block == 0 || 
  599.                   contained_in (block_found, innermost_block))
  600.                 innermost_block = block_found;
  601.                   write_exp_elt_opcode (OP_THIS);
  602.                   write_exp_elt_opcode (OP_THIS);
  603.                   write_exp_elt_opcode (STRUCTOP_PTR);
  604.                   write_exp_string ($1);
  605.                   write_exp_elt_opcode (STRUCTOP_PTR);
  606.                 }
  607.               else
  608.                 {
  609.                   register int i;
  610.                   register char *arg = copy_name ($1);
  611.  
  612.                   for (i = 0; i < misc_function_count; i++)
  613.                 if (!strcmp (misc_function_vector[i].name, arg))
  614.                   break;
  615.  
  616.                   if (i < misc_function_count)
  617.                 {
  618.                   enum misc_function_type mft =
  619.                     (enum misc_function_type)
  620.                       misc_function_vector[i].type;
  621.                   
  622.                   write_exp_elt_opcode (OP_LONG);
  623.                   write_exp_elt_type (builtin_type_int);
  624.                   write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
  625.                   write_exp_elt_opcode (OP_LONG);
  626.                   write_exp_elt_opcode (UNOP_MEMVAL);
  627.                   if (mft == mf_data || mft == mf_bss)
  628.                     write_exp_elt_type (builtin_type_int);
  629.                   else if (mft == mf_text)
  630.                     write_exp_elt_type (lookup_function_type (builtin_type_int));
  631.                   else
  632.                     write_exp_elt_type (builtin_type_char);
  633.                   write_exp_elt_opcode (UNOP_MEMVAL);
  634.                 }
  635.                   else if (symtab_list == 0
  636.                        && partial_symtab_list == 0)
  637.                 error ("No symbol table is loaded.  Use the \"symbol-file\" command.");
  638.                   else
  639.                 error ("No symbol \"%s\" in current context.",
  640.                        copy_name ($1));
  641.                 }
  642.             }
  643.     ;
  644.  
  645.  
  646. ptype    :    typebase
  647.     |    typebase abs_decl
  648.         {
  649.           /* This is where the interesting stuff happens.  */
  650.           int done = 0;
  651.           int array_size;
  652.           struct type *follow_type = $1;
  653.           
  654.           while (!done)
  655.             switch (pop_type ())
  656.               {
  657.               case tp_end:
  658.             done = 1;
  659.             break;
  660.               case tp_pointer:
  661.             follow_type = lookup_pointer_type (follow_type);
  662.             break;
  663.               case tp_reference:
  664.             follow_type = lookup_reference_type (follow_type);
  665.             break;
  666.               case tp_array:
  667.             array_size = (int) pop_type ();
  668.             if (array_size != -1)
  669.               follow_type = create_array_type (follow_type,
  670.                                array_size);
  671.             else
  672.               follow_type = lookup_pointer_type (follow_type);
  673.             break;
  674.               case tp_function:
  675.             follow_type = lookup_function_type (follow_type);
  676.             break;
  677.               }
  678.           $$ = follow_type;
  679.         }
  680.     ;
  681.  
  682. abs_decl:    '*'
  683.             { push_type (tp_pointer); $$ = 0; }
  684.     |    '*' abs_decl
  685.             { push_type (tp_pointer); $$ = $2; }
  686.     |    direct_abs_decl
  687.     ;
  688.  
  689. direct_abs_decl: '(' abs_decl ')'
  690.             { $$ = $2; }
  691.     |    direct_abs_decl array_mod
  692.             {
  693.               push_type ((enum type_pieces) $2);
  694.               push_type (tp_array);
  695.             }
  696.     |    array_mod
  697.             {
  698.               push_type ((enum type_pieces) $1);
  699.               push_type (tp_array);
  700.               $$ = 0;
  701.             }
  702.     |     direct_abs_decl func_mod
  703.             { push_type (tp_function); }
  704.     |    func_mod
  705.             { push_type (tp_function); }
  706.     ;
  707.  
  708. array_mod:    '[' ']'
  709.             { $$ = -1; }
  710.     |    '[' INT ']'
  711.             { $$ = $2; }
  712.     ;
  713.  
  714. func_mod:    '(' ')'
  715.             { $$ = 0; }
  716.     ;
  717.  
  718. type    :    ptype
  719.     |    typebase COLONCOLON '*'
  720.             { $$ = lookup_member_type (builtin_type_int, $1); }
  721.     |    type '(' typebase COLONCOLON '*' ')'
  722.             { $$ = lookup_member_type ($1, $3); }
  723.     |    type '(' typebase COLONCOLON '*' ')' '(' ')'
  724.             { $$ = lookup_member_type
  725.                 (lookup_function_type ($1), $3); }
  726.     |    type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
  727.             { $$ = lookup_member_type
  728.                 (lookup_function_type ($1), $3);
  729.               free ($8); }
  730.     ;
  731.  
  732. typebase
  733.     :    TYPENAME
  734.             { $$ = lookup_typename (copy_name ($1),
  735.                         expression_context_block, 0); }
  736.     |    INT_KEYWORD
  737.             { $$ = builtin_type_int; }
  738.     |    LONG
  739.             { $$ = builtin_type_long; }
  740.     |    SHORT
  741.             { $$ = builtin_type_short; }
  742.     |    LONG INT_KEYWORD
  743.             { $$ = builtin_type_long; }
  744.     |    UNSIGNED LONG INT_KEYWORD
  745.             { $$ = builtin_type_unsigned_long; }
  746.     |    SHORT INT_KEYWORD
  747.             { $$ = builtin_type_short; }
  748.     |    UNSIGNED SHORT INT_KEYWORD
  749.             { $$ = builtin_type_unsigned_short; }
  750.     |    STRUCT name
  751.             { $$ = lookup_struct (copy_name ($2),
  752.                           expression_context_block); }
  753.     |    UNION name
  754.             { $$ = lookup_union (copy_name ($2),
  755.                          expression_context_block); }
  756.     |    ENUM name
  757.             { $$ = lookup_enum (copy_name ($2),
  758.                         expression_context_block); }
  759.     |    UNSIGNED typename
  760.             { $$ = lookup_unsigned_typename (copy_name ($2)); }
  761.     |    UNSIGNED
  762.             { $$ = builtin_type_unsigned_int; }
  763.     |    SIGNED typename
  764.             { $$ = lookup_typename (copy_name ($2),
  765.                         expression_context_block, 0); }
  766.     |    SIGNED
  767.             { $$ = builtin_type_int; }
  768.     ;
  769.  
  770. typename:    TYPENAME
  771.     |    INT_KEYWORD
  772.         {
  773.           $$.ptr = "int";
  774.           $$.length = 3;
  775.         }
  776.     |    LONG
  777.         {
  778.           $$.ptr = "long";
  779.           $$.length = 4;
  780.         }
  781.     |    SHORT
  782.         {
  783.           $$.ptr = "short";
  784.           $$.length = 5;
  785.         }
  786.     ;
  787.  
  788. nonempty_typelist
  789.     :    type
  790.         { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2);
  791.           $$[0] = (struct type *)0;
  792.           $$[1] = $1;
  793.         }
  794.     |    nonempty_typelist ',' type
  795.         { int len = sizeof (struct type *) * ++($<ivec>1[0]);
  796.           $$ = (struct type **)xrealloc ($1, len);
  797.           $$[$<ivec>$[0]] = $3;
  798.         }
  799.     ;
  800.  
  801. name    :    NAME
  802.     |    BLOCKNAME
  803.     |    TYPENAME
  804.     ;
  805.  
  806. name_not_typename :    NAME
  807.     |    BLOCKNAME
  808.     ;
  809.  
  810. %%
  811.  
  812. /* Begin counting arguments for a function call,
  813.    saving the data about any containing call.  */
  814.  
  815. static void
  816. start_arglist ()
  817. {
  818.   register struct funcall *new = (struct funcall *) xmalloc (sizeof (struct funcall));
  819.  
  820.   new->next = funcall_chain;
  821.   new->arglist_len = arglist_len;
  822.   arglist_len = 0;
  823.   funcall_chain = new;
  824. }
  825.  
  826. /* Return the number of arguments in a function call just terminated,
  827.    and restore the data for the containing function call.  */
  828.  
  829. static int
  830. end_arglist ()
  831. {
  832.   register int val = arglist_len;
  833.   register struct funcall *call = funcall_chain;
  834.   funcall_chain = call->next;
  835.   arglist_len = call->arglist_len;
  836.   free (call);
  837.   return val;
  838. }
  839.  
  840. /* Free everything in the funcall chain.
  841.    Used when there is an error inside parsing.  */
  842.  
  843. static void
  844. free_funcalls ()
  845. {
  846.   register struct funcall *call, *next;
  847.  
  848.   for (call = funcall_chain; call; call = next)
  849.     {
  850.       next = call->next;
  851.       free (call);
  852.     }
  853. }
  854.  
  855. /* This page contains the functions for adding data to the  struct expression
  856.    being constructed.  */
  857.  
  858. /* Add one element to the end of the expression.  */
  859.  
  860. /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
  861.    a register through here */
  862.  
  863. static void
  864. write_exp_elt (expelt)
  865.      union exp_element expelt;
  866. {
  867.   if (expout_ptr >= expout_size)
  868.     {
  869.       expout_size *= 2;
  870.       expout = (struct expression *) xrealloc (expout,
  871.                            sizeof (struct expression)
  872.                            + expout_size * sizeof (union exp_element));
  873.     }
  874.   expout->elts[expout_ptr++] = expelt;
  875. }
  876.  
  877. static void
  878. write_exp_elt_opcode (expelt)
  879.      enum exp_opcode expelt;
  880. {
  881.   union exp_element tmp;
  882.  
  883.   tmp.opcode = expelt;
  884.  
  885.   write_exp_elt (tmp);
  886. }
  887.  
  888. static void
  889. write_exp_elt_sym (expelt)
  890.      struct symbol *expelt;
  891. {
  892.   union exp_element tmp;
  893.  
  894.   tmp.symbol = expelt;
  895.  
  896.   write_exp_elt (tmp);
  897. }
  898.  
  899. static void
  900. write_exp_elt_longcst (expelt)
  901.      LONGEST expelt;
  902. {
  903.   union exp_element tmp;
  904.  
  905.   tmp.longconst = expelt;
  906.  
  907.   write_exp_elt (tmp);
  908. }
  909.  
  910. static void
  911. write_exp_elt_dblcst (expelt)
  912.      double expelt;
  913. {
  914.   union exp_element tmp;
  915.  
  916.   tmp.doubleconst = expelt;
  917.  
  918.   write_exp_elt (tmp);
  919. }
  920.  
  921. static void
  922. write_exp_elt_type (expelt)
  923.      struct type *expelt;
  924. {
  925.   union exp_element tmp;
  926.  
  927.   tmp.type = expelt;
  928.  
  929.   write_exp_elt (tmp);
  930. }
  931.  
  932. static void
  933. write_exp_elt_intern (expelt)
  934.      struct internalvar *expelt;
  935. {
  936.   union exp_element tmp;
  937.  
  938.   tmp.internalvar = expelt;
  939.  
  940.   write_exp_elt (tmp);
  941. }
  942.  
  943. /* Add a string constant to the end of the expression.
  944.    Follow it by its length in bytes, as a separate exp_element.  */
  945.  
  946. static void
  947. write_exp_string (str)
  948.      struct stoken str;
  949. {
  950.   register int len = str.length;
  951.   register int lenelt
  952.     = (len + sizeof (union exp_element)) / sizeof (union exp_element);
  953.  
  954.   expout_ptr += lenelt;
  955.  
  956.   if (expout_ptr >= expout_size)
  957.     {
  958.       expout_size = max (expout_size * 2, expout_ptr + 10);
  959.       expout = (struct expression *)
  960.     xrealloc (expout, (sizeof (struct expression)
  961.                + (expout_size * sizeof (union exp_element))));
  962.     }
  963.   bcopy (str.ptr, (char *) &expout->elts[expout_ptr - lenelt], len);
  964.   ((char *) &expout->elts[expout_ptr - lenelt])[len] = 0;
  965.   write_exp_elt_longcst ((LONGEST) len);
  966. }
  967.  
  968. /* During parsing of a C expression, the pointer to the next character
  969.    is in this variable.  */
  970.  
  971. static char *lexptr;
  972.  
  973. /* Tokens that refer to names do so with explicit pointer and length,
  974.    so they can share the storage that lexptr is parsing.
  975.  
  976.    When it is necessary to pass a name to a function that expects
  977.    a null-terminated string, the substring is copied out
  978.    into a block of storage that namecopy points to.
  979.  
  980.    namecopy is allocated once, guaranteed big enough, for each parsing.  */
  981.  
  982. static char *namecopy;
  983.  
  984. /* Current depth in parentheses within the expression.  */
  985.  
  986. static int paren_depth;
  987.  
  988. /* Nonzero means stop parsing on first comma (if not within parentheses).  */
  989.  
  990. static int comma_terminates;
  991.  
  992. /* Take care of parsing a number (anything that starts with a digit).
  993.    Set yylval and return the token type; update lexptr.
  994.    LEN is the number of characters in it.  */
  995.  
  996. /*** Needs some error checking for the float case ***/
  997.  
  998. static int
  999. parse_number (olen)
  1000.      int olen;
  1001. {
  1002.   register char *p = lexptr;
  1003.   register LONGEST n = 0;
  1004.   register int c;
  1005.   register int base = 10;
  1006.   register int len = olen;
  1007.   char *err_copy;
  1008.   int unsigned_p = 0;
  1009.  
  1010.   extern double atof ();
  1011.  
  1012.   for (c = 0; c < len; c++)
  1013.     if (p[c] == '.')
  1014.       {
  1015.     /* It's a float since it contains a point.  */
  1016.     yylval.dval = atof (p);
  1017.     lexptr += len;
  1018.     return FLOAT;
  1019.       }
  1020.  
  1021.   if (len >= 3 && (!strncmp (p, "0x", 2) || !strncmp (p, "0X", 2)))
  1022.     {
  1023.       p += 2;
  1024.       base = 16;
  1025.       len -= 2;
  1026.     }
  1027.   else if (*p == '0')
  1028.     base = 8;
  1029.  
  1030.   while (len-- > 0)
  1031.     {
  1032.       c = *p++;
  1033.       if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
  1034.       if (c != 'l' && c != 'u')
  1035.     n *= base;
  1036.       if (c >= '0' && c <= '9')
  1037.     n += c - '0';
  1038.       else
  1039.     {
  1040.       if (base == 16 && c >= 'a' && c <= 'f')
  1041.         n += c - 'a' + 10;
  1042.       else if (len == 0 && c == 'l')
  1043.         ;
  1044.       else if (len == 0 && c == 'u')
  1045.         unsigned_p = 1;
  1046.       else if (base == 10 && len != 0 && (c == 'e' || c == 'E'))
  1047.         {
  1048.           /* Scientific notation, where we are unlucky enough not
  1049.          to have a '.' in the string.  */
  1050.           yylval.dval = atof (lexptr);
  1051.           lexptr += olen;
  1052.           return FLOAT;
  1053.         }
  1054.       else
  1055.         {
  1056.           err_copy = (char *) alloca (olen + 1);
  1057.           bcopy (lexptr, err_copy, olen);
  1058.           err_copy[olen] = 0;
  1059.           error ("Invalid number \"%s\".", err_copy);
  1060.         }
  1061.     }
  1062.     }
  1063.  
  1064.   lexptr = p;
  1065.   if (unsigned_p)
  1066.     {
  1067.       yylval.ulval = n;
  1068.       return UINT;
  1069.     }
  1070.   else
  1071.     {
  1072.       yylval.lval = n;
  1073.       return INT;
  1074.     }
  1075. }
  1076.  
  1077. struct token
  1078. {
  1079.   char *operator;
  1080.   int token;
  1081.   enum exp_opcode opcode;
  1082. };
  1083.  
  1084. static struct token tokentab3[] =
  1085.   {
  1086.     {">>=", ASSIGN_MODIFY, BINOP_RSH},
  1087.     {"<<=", ASSIGN_MODIFY, BINOP_LSH}
  1088.   };
  1089.  
  1090. static struct token tokentab2[] =
  1091.   {
  1092.     {"+=", ASSIGN_MODIFY, BINOP_ADD},
  1093.     {"-=", ASSIGN_MODIFY, BINOP_SUB},
  1094.     {"*=", ASSIGN_MODIFY, BINOP_MUL},
  1095.     {"/=", ASSIGN_MODIFY, BINOP_DIV},
  1096.     {"%=", ASSIGN_MODIFY, BINOP_REM},
  1097.     {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
  1098.     {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
  1099.     {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
  1100.     {"++", INCREMENT, BINOP_END},
  1101.     {"--", DECREMENT, BINOP_END},
  1102.     {"->", ARROW, BINOP_END},
  1103.     {"&&", AND, BINOP_END},
  1104.     {"||", OR, BINOP_END},
  1105.     {"::", COLONCOLON, BINOP_END},
  1106.     {"<<", LSH, BINOP_END},
  1107.     {">>", RSH, BINOP_END},
  1108.     {"==", EQUAL, BINOP_END},
  1109.     {"!=", NOTEQUAL, BINOP_END},
  1110.     {"<=", LEQ, BINOP_END},
  1111.     {">=", GEQ, BINOP_END}
  1112.   };
  1113.  
  1114. /* assign machine-independent names to certain registers 
  1115.  * (unless overridden by the REGISTER_NAMES table)
  1116.  */
  1117. struct std_regs {
  1118.     char *name;
  1119.     int regnum;
  1120. } std_regs[] = {
  1121. #ifdef PC_REGNUM
  1122.     { "pc", PC_REGNUM },
  1123. #endif
  1124. #ifdef FP_REGNUM
  1125.     { "fp", FP_REGNUM },
  1126. #endif
  1127. #ifdef SP_REGNUM
  1128.     { "sp", SP_REGNUM },
  1129. #endif
  1130. #ifdef PS_REGNUM
  1131.     { "ps", PS_REGNUM },
  1132. #endif
  1133. };
  1134.  
  1135. #define NUM_STD_REGS (sizeof std_regs / sizeof std_regs[0])
  1136.  
  1137. /* Read one token, getting characters through lexptr.  */
  1138.  
  1139. static int
  1140. yylex ()
  1141. {
  1142.   register int c;
  1143.   register int namelen;
  1144.   register int i;
  1145.   register char *tokstart;
  1146.  
  1147.  retry:
  1148.  
  1149.   tokstart = lexptr;
  1150.   /* See if it is a special token of length 3.  */
  1151.   for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
  1152.     if (!strncmp (tokstart, tokentab3[i].operator, 3))
  1153.       {
  1154.     lexptr += 3;
  1155.     yylval.opcode = tokentab3[i].opcode;
  1156.     return tokentab3[i].token;
  1157.       }
  1158.  
  1159.   /* See if it is a special token of length 2.  */
  1160.   for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
  1161.     if (!strncmp (tokstart, tokentab2[i].operator, 2))
  1162.       {
  1163.     lexptr += 2;
  1164.     yylval.opcode = tokentab2[i].opcode;
  1165.     return tokentab2[i].token;
  1166.       }
  1167.  
  1168.   switch (c = *tokstart)
  1169.     {
  1170.     case 0:
  1171.       return 0;
  1172.  
  1173.     case ' ':
  1174.     case '\t':
  1175.     case '\n':
  1176.       lexptr++;
  1177.       goto retry;
  1178.  
  1179.     case '\'':
  1180.       lexptr++;
  1181.       c = *lexptr++;
  1182.       if (c == '\\')
  1183.     c = parse_escape (&lexptr);
  1184.       yylval.lval = c;
  1185.       c = *lexptr++;
  1186.       if (c != '\'')
  1187.     error ("Invalid character constant.");
  1188.       return CHAR;
  1189.  
  1190.     case '(':
  1191.       paren_depth++;
  1192.       lexptr++;
  1193.       return c;
  1194.  
  1195.     case ')':
  1196.       if (paren_depth == 0)
  1197.     return 0;
  1198.       paren_depth--;
  1199.       lexptr++;
  1200.       return c;
  1201.  
  1202.     case ',':
  1203.       if (comma_terminates && paren_depth == 0)
  1204.     return 0;
  1205.       lexptr++;
  1206.       return c;
  1207.  
  1208.     case '.':
  1209.       /* Might be a floating point number.  */
  1210.       if (lexptr[1] >= '0' && lexptr[1] <= '9')
  1211.     break;            /* Falls into number code.  */
  1212.  
  1213.     case '+':
  1214.     case '-':
  1215.     case '*':
  1216.     case '/':
  1217.     case '%':
  1218.     case '|':
  1219.     case '&':
  1220.     case '^':
  1221.     case '~':
  1222.     case '!':
  1223.     case '@':
  1224.     case '<':
  1225.     case '>':
  1226.     case '[':
  1227.     case ']':
  1228.     case '?':
  1229.     case ':':
  1230.     case '=':
  1231.     case '{':
  1232.     case '}':
  1233.       lexptr++;
  1234.       return c;
  1235.  
  1236.     case '"':
  1237.       for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
  1238.     if (c == '\\')
  1239.       {
  1240.         c = tokstart[++namelen];
  1241.         if (c >= '0' && c <= '9')
  1242.           {
  1243.         c = tokstart[++namelen];
  1244.         if (c >= '0' && c <= '9')
  1245.           c = tokstart[++namelen];
  1246.           }
  1247.       }
  1248.       yylval.sval.ptr = tokstart + 1;
  1249.       yylval.sval.length = namelen - 1;
  1250.       lexptr += namelen + 1;
  1251.       return STRING;
  1252.     }
  1253.  
  1254.   /* Is it a number?  */
  1255.   /* Note:  We have already dealt with the case of the token '.'.
  1256.      See case '.' above.  */
  1257.   if ((c >= '0' && c <= '9') || c == '.')
  1258.     {
  1259.       /* It's a number.  */
  1260.       int got_dot = 0, got_e = 0;
  1261.       register char *p = tokstart;
  1262.       int hex = c == '0' && (p[1] == 'x' || p[1] == 'X');
  1263.       if (hex)
  1264.     p += 2;
  1265.       for (;; ++p)
  1266.     {
  1267.       if (!hex && !got_e && (*p == 'e' || *p == 'E'))
  1268.         got_dot = got_e = 1;
  1269.       else if (!hex && !got_dot && *p == '.')
  1270.         got_dot = 1;
  1271.       else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
  1272.            && (*p == '-' || *p == '+'))
  1273.         /* This is the sign of the exponent, not the end of the
  1274.            number.  */
  1275.         continue;
  1276.       else if (*p < '0' || *p > '9'
  1277.            && (!hex || ((*p < 'a' || *p > 'f')
  1278.                 && (*p < 'A' || *p > 'F'))))
  1279.         break;
  1280.     }
  1281.       return parse_number (p - tokstart);
  1282.     }
  1283.  
  1284.   if (!(c == '_' || c == '$'
  1285.     || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
  1286.     /* We must have come across a bad character (e.g. ';').  */
  1287.     error ("Invalid character '%c' in expression.", c);
  1288.  
  1289.   /* It's a name.  See how long it is.  */
  1290.   namelen = 0;
  1291.   for (c = tokstart[namelen];
  1292.        (c == '_' || c == '$' || (c >= '0' && c <= '9')
  1293.     || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
  1294.        c = tokstart[++namelen])
  1295.     ;
  1296.  
  1297.   /* The token "if" terminates the expression and is NOT 
  1298.      removed from the input stream.  */
  1299.   if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
  1300.     {
  1301.       return 0;
  1302.     }
  1303.  
  1304.   lexptr += namelen;
  1305.  
  1306.   /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
  1307.      and $$digits (equivalent to $<-digits> if you could type that).
  1308.      Make token type LAST, and put the number (the digits) in yylval.  */
  1309.  
  1310.   if (*tokstart == '$')
  1311.     {
  1312.       register int negate = 0;
  1313.       c = 1;
  1314.       /* Double dollar means negate the number and add -1 as well.
  1315.      Thus $$ alone means -1.  */
  1316.       if (namelen >= 2 && tokstart[1] == '$')
  1317.     {
  1318.       negate = 1;
  1319.       c = 2;
  1320.     }
  1321.       if (c == namelen)
  1322.     {
  1323.       /* Just dollars (one or two) */
  1324.       yylval.lval = - negate;
  1325.       return LAST;
  1326.     }
  1327.       /* Is the rest of the token digits?  */
  1328.       for (; c < namelen; c++)
  1329.     if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
  1330.       break;
  1331.       if (c == namelen)
  1332.     {
  1333.       yylval.lval = atoi (tokstart + 1 + negate);
  1334.       if (negate)
  1335.         yylval.lval = - yylval.lval;
  1336.       return LAST;
  1337.     }
  1338.     }
  1339.  
  1340.   /* Handle tokens that refer to machine registers:
  1341.      $ followed by a register name.  */
  1342.  
  1343.   if (*tokstart == '$') {
  1344.     for (c = 0; c < NUM_REGS; c++)
  1345.       if (namelen - 1 == strlen (reg_names[c])
  1346.       && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
  1347.     {
  1348.       yylval.lval = c;
  1349.       return REGNAME;
  1350.     }
  1351.     for (c = 0; c < NUM_STD_REGS; c++)
  1352.      if (namelen - 1 == strlen (std_regs[c].name)
  1353.      && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
  1354.        {
  1355.      yylval.lval = std_regs[c].regnum;
  1356.      return REGNAME;
  1357.        }
  1358.   }
  1359.   /* Catch specific keywords.  Should be done with a data structure.  */
  1360.   switch (namelen)
  1361.     {
  1362.     case 8:
  1363.       if (!strncmp (tokstart, "unsigned", 8))
  1364.     return UNSIGNED;
  1365.       break;
  1366.     case 6:
  1367.       if (!strncmp (tokstart, "struct", 6))
  1368.     return STRUCT;
  1369.       if (!strncmp (tokstart, "signed", 6))
  1370.     return SIGNED;
  1371.       if (!strncmp (tokstart, "sizeof", 6))      
  1372.     return SIZEOF;
  1373.       break;
  1374.     case 5:
  1375.       if (!strncmp (tokstart, "union", 5))
  1376.     return UNION;
  1377.       if (!strncmp (tokstart, "short", 5))
  1378.     return SHORT;
  1379.       break;
  1380.     case 4:
  1381.       if (!strncmp (tokstart, "enum", 4))
  1382.     return ENUM;
  1383.       if (!strncmp (tokstart, "long", 4))
  1384.     return LONG;
  1385.       if (!strncmp (tokstart, "this", 4)
  1386.       && lookup_symbol ("$this", expression_context_block,
  1387.                 VAR_NAMESPACE, 0))
  1388.     return THIS;
  1389.       break;
  1390.     case 3:
  1391.       if (!strncmp (tokstart, "int", 3))
  1392.     return INT_KEYWORD;
  1393.       break;
  1394.     default:
  1395.       break;
  1396.     }
  1397.  
  1398.   yylval.sval.ptr = tokstart;
  1399.   yylval.sval.length = namelen;
  1400.  
  1401.   /* Any other names starting in $ are debugger internal variables.  */
  1402.  
  1403.   if (*tokstart == '$')
  1404.     {
  1405.       yylval.ivar = (struct internalvar *) lookup_internalvar (copy_name (yylval.sval) + 1);
  1406.       return VARIABLE;
  1407.     }
  1408.  
  1409.   /* Use token-type BLOCKNAME for symbols that happen to be defined as
  1410.      functions or symtabs.  If this is not so, then ...
  1411.      Use token-type TYPENAME for symbols that happen to be defined
  1412.      currently as names of types; NAME for other symbols.
  1413.      The caller is not constrained to care about the distinction.  */
  1414.   {
  1415.     char *tmp = copy_name (yylval.sval);
  1416.     struct symbol *sym;
  1417.  
  1418.     if (lookup_partial_symtab (tmp))
  1419.       return BLOCKNAME;
  1420.     sym = lookup_symbol (tmp, expression_context_block,
  1421.              VAR_NAMESPACE, 0);
  1422.     if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
  1423.       return BLOCKNAME;
  1424.     if (lookup_typename (copy_name (yylval.sval), expression_context_block, 1))
  1425.       return TYPENAME;
  1426.     return NAME;
  1427.   }
  1428. }
  1429.  
  1430. static void
  1431. yyerror ()
  1432. {
  1433.   error ("Invalid syntax in expression.");
  1434. }
  1435.  
  1436. /* Return a null-terminated temporary copy of the name
  1437.    of a string token.  */
  1438.  
  1439. static char *
  1440. copy_name (token)
  1441.      struct stoken token;
  1442. {
  1443.   bcopy (token.ptr, namecopy, token.length);
  1444.   namecopy[token.length] = 0;
  1445.   return namecopy;
  1446. }
  1447.  
  1448. /* Reverse an expression from suffix form (in which it is constructed)
  1449.    to prefix form (in which we can conveniently print or execute it).  */
  1450.  
  1451. static void prefixify_subexp ();
  1452.  
  1453. static void
  1454. prefixify_expression (expr)
  1455.      register struct expression *expr;
  1456. {
  1457.   register int len = sizeof (struct expression) +
  1458.                     expr->nelts * sizeof (union exp_element);
  1459.   register struct expression *temp;
  1460.   register int inpos = expr->nelts, outpos = 0;
  1461.  
  1462.   temp = (struct expression *) alloca (len);
  1463.  
  1464.   /* Copy the original expression into temp.  */
  1465.   bcopy (expr, temp, len);
  1466.  
  1467.   prefixify_subexp (temp, expr, inpos, outpos);
  1468. }
  1469.  
  1470. /* Return the number of exp_elements in the subexpression of EXPR
  1471.    whose last exp_element is at index ENDPOS - 1 in EXPR.  */
  1472.  
  1473. static int
  1474. length_of_subexp (expr, endpos)
  1475.      register struct expression *expr;
  1476.      register int endpos;
  1477. {
  1478.   register int oplen = 1;
  1479.   register int args = 0;
  1480.   register int i;
  1481.  
  1482.   if (endpos < 0)
  1483.     error ("?error in length_of_subexp");
  1484.  
  1485.   i = (int) expr->elts[endpos - 1].opcode;
  1486.  
  1487.   switch (i)
  1488.     {
  1489.       /* C++  */
  1490.     case OP_SCOPE:
  1491.       oplen = 4 + ((expr->elts[endpos - 2].longconst
  1492.             + sizeof (union exp_element))
  1493.            / sizeof (union exp_element));
  1494.       break;
  1495.  
  1496.     case OP_LONG:
  1497.     case OP_DOUBLE:
  1498.       oplen = 4;
  1499.       break;
  1500.  
  1501.     case OP_VAR_VALUE:
  1502.     case OP_LAST:
  1503.     case OP_REGISTER:
  1504.     case OP_INTERNALVAR:
  1505.       oplen = 3;
  1506.       break;
  1507.  
  1508.     case OP_FUNCALL:
  1509.       oplen = 3;
  1510.       args = 1 + expr->elts[endpos - 2].longconst;
  1511.       break;
  1512.  
  1513.     case UNOP_CAST:
  1514.     case UNOP_MEMVAL:
  1515.       oplen = 3;
  1516.       args = 1;
  1517.       break;
  1518.  
  1519.     case STRUCTOP_STRUCT:
  1520.     case STRUCTOP_PTR:
  1521.       args = 1;
  1522.     case OP_STRING:
  1523.       oplen = 3 + ((expr->elts[endpos - 2].longconst
  1524.             + sizeof (union exp_element))
  1525.            / sizeof (union exp_element));
  1526.       break;
  1527.  
  1528.     case TERNOP_COND:
  1529.       args = 3;
  1530.       break;
  1531.  
  1532.     case BINOP_ASSIGN_MODIFY:
  1533.       oplen = 3;
  1534.       args = 2;
  1535.       break;
  1536.  
  1537.       /* C++ */
  1538.     case OP_THIS:
  1539.       oplen = 2;
  1540.       break;
  1541.  
  1542.     default:
  1543.       args = 1 + (i < (int) BINOP_END);
  1544.     }
  1545.  
  1546.   while (args > 0)
  1547.     {
  1548.       oplen += length_of_subexp (expr, endpos - oplen);
  1549.       args--;
  1550.     }
  1551.  
  1552.   return oplen;
  1553. }
  1554.  
  1555. /* Copy the subexpression ending just before index INEND in INEXPR
  1556.    into OUTEXPR, starting at index OUTBEG.
  1557.    In the process, convert it from suffix to prefix form.  */
  1558.  
  1559. static void
  1560. prefixify_subexp (inexpr, outexpr, inend, outbeg)
  1561.      register struct expression *inexpr;
  1562.      struct expression *outexpr;
  1563.      register int inend;
  1564.      int outbeg;
  1565. {
  1566.   register int oplen = 1;
  1567.   register int args = 0;
  1568.   register int i;
  1569.   int *arglens;
  1570.   enum exp_opcode opcode;
  1571.  
  1572.   /* Compute how long the last operation is (in OPLEN),
  1573.      and also how many preceding subexpressions serve as
  1574.      arguments for it (in ARGS).  */
  1575.  
  1576.   opcode = inexpr->elts[inend - 1].opcode;
  1577.   switch (opcode)
  1578.     {
  1579.       /* C++  */
  1580.     case OP_SCOPE:
  1581.       oplen = 4 + ((inexpr->elts[inend - 2].longconst
  1582.             + sizeof (union exp_element))
  1583.            / sizeof (union exp_element));
  1584.       break;
  1585.  
  1586.     case OP_LONG:
  1587.     case OP_DOUBLE:
  1588.       oplen = 4;
  1589.       break;
  1590.  
  1591.     case OP_VAR_VALUE:
  1592.     case OP_LAST:
  1593.     case OP_REGISTER:
  1594.     case OP_INTERNALVAR:
  1595.       oplen = 3;
  1596.       break;
  1597.  
  1598.     case OP_FUNCALL:
  1599.       oplen = 3;
  1600.       args = 1 + inexpr->elts[inend - 2].longconst;
  1601.       break;
  1602.  
  1603.     case UNOP_CAST:
  1604.     case UNOP_MEMVAL:
  1605.       oplen = 3;
  1606.       args = 1;
  1607.       break;
  1608.  
  1609.     case STRUCTOP_STRUCT:
  1610.     case STRUCTOP_PTR:
  1611.       args = 1;
  1612.     case OP_STRING:
  1613.       oplen = 3 + ((inexpr->elts[inend - 2].longconst
  1614.             + sizeof (union exp_element))
  1615.            / sizeof (union exp_element));
  1616.            
  1617.       break;
  1618.  
  1619.     case TERNOP_COND:
  1620.       args = 3;
  1621.       break;
  1622.  
  1623.     case BINOP_ASSIGN_MODIFY:
  1624.       oplen = 3;
  1625.       args = 2;
  1626.       break;
  1627.  
  1628.       /* C++ */
  1629.     case OP_THIS:
  1630.       oplen = 2;
  1631.       break;
  1632.  
  1633.     default:
  1634.       args = 1 + ((int) opcode < (int) BINOP_END);
  1635.     }
  1636.  
  1637.   /* Copy the final operator itself, from the end of the input
  1638.      to the beginning of the output.  */
  1639.   inend -= oplen;
  1640.   bcopy (&inexpr->elts[inend], &outexpr->elts[outbeg],
  1641.      oplen * sizeof (union exp_element));
  1642.   outbeg += oplen;
  1643.  
  1644.   /* Find the lengths of the arg subexpressions.  */
  1645.   arglens = (int *) alloca (args * sizeof (int));
  1646.   for (i = args - 1; i >= 0; i--)
  1647.     {
  1648.       oplen = length_of_subexp (inexpr, inend);
  1649.       arglens[i] = oplen;
  1650.       inend -= oplen;
  1651.     }
  1652.  
  1653.   /* Now copy each subexpression, preserving the order of
  1654.      the subexpressions, but prefixifying each one.
  1655.      In this loop, inend starts at the beginning of
  1656.      the expression this level is working on
  1657.      and marches forward over the arguments.
  1658.      outbeg does similarly in the output.  */
  1659.   for (i = 0; i < args; i++)
  1660.     {
  1661.       oplen = arglens[i];
  1662.       inend += oplen;
  1663.       prefixify_subexp (inexpr, outexpr, inend, outbeg);
  1664.       outbeg += oplen;
  1665.     }
  1666. }
  1667.  
  1668. /* This page contains the two entry points to this file.  */
  1669.  
  1670. /* Read a C expression from the string *STRINGPTR points to,
  1671.    parse it, and return a pointer to a  struct expression  that we malloc.
  1672.    Use block BLOCK as the lexical context for variable names;
  1673.    if BLOCK is zero, use the block of the selected stack frame.
  1674.    Meanwhile, advance *STRINGPTR to point after the expression,
  1675.    at the first nonwhite character that is not part of the expression
  1676.    (possibly a null character).
  1677.  
  1678.    If COMMA is nonzero, stop if a comma is reached.  */
  1679.  
  1680. struct expression *
  1681. parse_c_1 (stringptr, block, comma)
  1682.      char **stringptr;
  1683.      struct block *block;
  1684. {
  1685.   struct cleanup *old_chain;
  1686.  
  1687.   lexptr = *stringptr;
  1688.  
  1689.   paren_depth = 0;
  1690.   type_stack_depth = 0;
  1691.  
  1692.   comma_terminates = comma;
  1693.  
  1694.   if (lexptr == 0 || *lexptr == 0)
  1695.     error_no_arg ("expression to compute");
  1696.  
  1697.   old_chain = make_cleanup (free_funcalls, 0);
  1698.   funcall_chain = 0;
  1699.  
  1700.   expression_context_block = block ? block : get_selected_block ();
  1701.  
  1702.   namecopy = (char *) alloca (strlen (lexptr) + 1);
  1703.   expout_size = 10;
  1704.   expout_ptr = 0;
  1705.   expout = (struct expression *)
  1706.     xmalloc (sizeof (struct expression)
  1707.          + expout_size * sizeof (union exp_element));
  1708.   make_cleanup (free_current_contents, &expout);
  1709.   if (yyparse ())
  1710.     yyerror ();
  1711.   discard_cleanups (old_chain);
  1712.   expout->nelts = expout_ptr;
  1713.   expout = (struct expression *)
  1714.     xrealloc (expout,
  1715.           sizeof (struct expression)
  1716.           + expout_ptr * sizeof (union exp_element));
  1717.   prefixify_expression (expout);
  1718.   *stringptr = lexptr;
  1719.   return expout;
  1720. }
  1721.  
  1722. /* Parse STRING as an expression, and complain if this fails
  1723.    to use up all of the contents of STRING.  */
  1724.  
  1725. struct expression *
  1726. parse_c_expression (string)
  1727.      char *string;
  1728. {
  1729.   register struct expression *exp;
  1730.   exp = parse_c_1 (&string, 0, 0);
  1731.   if (*string)
  1732.     error ("Junk after end of expression.");
  1733.   return exp;
  1734. }
  1735.  
  1736. static void 
  1737. push_type (tp)
  1738.      enum type_pieces tp;
  1739. {
  1740.   if (type_stack_depth == type_stack_size)
  1741.     {
  1742.       type_stack_size *= 2;
  1743.       type_stack = (enum type_pieces *)
  1744.     xrealloc (type_stack, type_stack_size * sizeof (enum type_pieces));
  1745.     }
  1746.   type_stack[type_stack_depth++] = tp;
  1747. }
  1748.  
  1749. static enum type_pieces 
  1750. pop_type ()
  1751. {
  1752.   if (type_stack_depth)
  1753.     return type_stack[--type_stack_depth];
  1754.   return tp_end;
  1755. }
  1756.  
  1757. void
  1758. _initialize_expread ()
  1759. {
  1760.   type_stack_size = 80;
  1761.   type_stack_depth = 0;
  1762.   type_stack = (enum type_pieces *)
  1763.     xmalloc (type_stack_size * sizeof (enum type_pieces));
  1764. }
  1765.